What is mersenne-twister?
The 'mersenne-twister' npm package is a JavaScript implementation of the Mersenne Twister algorithm, which is a pseudorandom number generator (PRNG). It is widely used for generating high-quality random numbers in various applications, including simulations, games, and statistical sampling.
What are mersenne-twister's main functionalities?
Basic Random Number Generation
This feature allows you to generate a random number between 0 and 1. The Mersenne Twister algorithm ensures that the numbers are uniformly distributed and have a long period, making them suitable for various applications.
const MersenneTwister = require('mersenne-twister');
const generator = new MersenneTwister();
const randomNumber = generator.random();
console.log(randomNumber);
Seeding the Generator
You can seed the Mersenne Twister generator with a specific value to ensure reproducibility of the random number sequence. This is useful for debugging and testing purposes.
const MersenneTwister = require('mersenne-twister');
const seed = 12345;
const generator = new MersenneTwister(seed);
const randomNumber = generator.random();
console.log(randomNumber);
Generating Random Integers
This feature demonstrates how to generate random integers within a specified range. By scaling and flooring the output of the generator, you can obtain random integers suitable for various applications.
const MersenneTwister = require('mersenne-twister');
const generator = new MersenneTwister();
const randomInt = Math.floor(generator.random() * 100);
console.log(randomInt);
Other packages similar to mersenne-twister
random-js
The 'random-js' package provides a variety of random number generators, including the Mersenne Twister. It offers more flexibility and additional features such as generating random booleans, picking random elements from an array, and shuffling arrays. Compared to 'mersenne-twister', 'random-js' is more versatile and feature-rich.
seedrandom
The 'seedrandom' package allows you to create seeded random number generators with various algorithms, including the Mersenne Twister. It supports multiple PRNG algorithms and provides a consistent API for generating random numbers. 'seedrandom' is more focused on providing a variety of algorithms and seeding options compared to 'mersenne-twister'.
pure-rand
The 'pure-rand' package offers a collection of pure random number generators, including the Mersenne Twister. It is designed to be fast and efficient, with a focus on functional programming. 'pure-rand' provides a more modern and functional approach to random number generation compared to 'mersenne-twister'.
Pseudorandom number generator
Mersenne Twister pseudorandom number generator.
Origin source (generator interface was changed)
Algorithm - http://en.wikipedia.org/wiki/Mersenne_twister
Installation
$ npm install mersenne-twister
Usage
var MersenneTwister = require('mersenne-twister');
var generator = new MersenneTwister();
generator.random();
generator.random_int();
generator.random_incl();
generator.random_excl();
generator.random_long();
generator.random_int31();
Seeding
If you want to use a specific seed in order to get a repeatable random sequence, pass an integer into the constructor:
var generator = new MersenneTwister(123);
and that will always produce the same random sequence.
Also you can do it on existing generator instance:
generator.init_seed(123);
License
See source